package com.hxzon.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Properties;

import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class EMailUtil {
	private static boolean check(EMail email) {
		if (email == null) {
			System.out.println("email is null");
			return false;
		}
		if (email.getHost() == null || email.getHost().isEmpty()) {
			System.out.println("host is null");
			return false;
		}
		if (email.getUsername() == null || email.getUsername().isEmpty()) {
			System.out.println("username is null");
			return false;
		}
		if (email.getPassword() == null || email.getPassword().isEmpty()) {
			System.out.println("password is null");
			return false;
		}
		if (email.getFrom() == null || email.getFrom().isEmpty()) {
			System.out.println("from is null");
			return false;
		}
		if (email.getTos().isEmpty()) {
			System.out.println("to is null");
			return false;
		}
		return true;
	}

	public static boolean send(EMail email) {
		return send(email, false);
	}

	public static boolean send(EMail email, boolean debug) {
		if (!check(email)) {
			return false;
		}
		Properties props = new Properties();
		// 设置发送邮件的邮件服务器的属性
		props.put("mail.smtp.host", email.getHost());
		Session session = Session.getDefaultInstance(props);
		session.setDebug(debug);
		MimeMessage message = new MimeMessage(session);
		try {
			message.setFrom(new InternetAddress(email.getFrom()));
			for (String to : email.tos) {
				message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
			}
			message.setSubject(email.getSubject()); // multipart对象中添加邮件的各个部分内容，包括文本和附件
			Multipart multipart = new MimeMultipart(); // 文本部分
			BodyPart textPart = new MimeBodyPart();
			textPart.setText(email.getContentText());// hxz
			multipart.addBodyPart(textPart);

			// // 附件部分
			// BodyPart affixPart = new MimeBodyPart();
			// DataSource affixSource = new FileDataSource(affix);
			// affixPart.setDataHandler(new DataHandler(affixSource));
			// // 添加附件的标题
			// // 这里很重要，通过下面的Base64编码的转换可以保证你的
			// // 中文附件标题名在发送时不会变成乱码
			// sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
			// affixPart.setFileName("=?GBK?B?" +
			// enc.encode(affixName.getBytes())
			// + "?=");
			// // affixPart.setFileName(affixName);//中文标题变成了乱码
			// multipart.addBodyPart(affixPart); // 将multipart对象放到message中

			message.setContent(multipart);
			// 保存邮件
			message.saveChanges();

			Transport transport = session.getTransport("smtp");// "smtp"
			// 连接服务器的邮箱
			transport.connect(email.getHost(), email.getUsername(), email.getPassword());
			// 把邮件发送出去
			transport.sendMessage(message, message.getAllRecipients());
			transport.close();
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	// public static void main(String[] args){
	// SendMail s=new SendMail();
	// s.setUser("smtp.163.com", "hxzon", "");
	// s.setHeader("hxzon@163.com", "xzon@qq.com", "发送邮件");
	// s.setContentText("hello");
	// s.send();
	//		
	// SendMail s=new SendMail();
	// s.setUser("mail.ptswitch.com", "huangxz@ptswitch.com", "8");
	// s.setHeader("huangxz@ptswitch.com", "xzon@qq.com", "发送邮件");
	// s.setContentText("hello");
	// s.send();
	// }

	public static class EMail {
		private String host; // smtp服务器
		private String username; // 用户名
		private String password; // 密码

		private String from; // 发件人地址
		private List<String> tos = new ArrayList<String>();// 收件人地址
		private String subject = ""; // 邮件标题

		private String contentText = "";// 邮件正文文本内容
		private String affix = ""; // 附件地址
		private String affixName = ""; // 附件名称

		public String getHost() {
			return host;
		}

		public void setHost(String host) {
			this.host = host;
		}

		public String getUsername() {
			return username;
		}

		public void setUsername(String username) {
			this.username = username;
		}

		public String getPassword() {
			return password;
		}

		public void setPassword(String password) {
			this.password = password;
		}

		public String getFrom() {
			return from;
		}

		public void setFrom(String from) {
			this.from = from;
		}

		public List<String> getTos() {
			return tos;
		}

		public void setTos(Collection<String> tos) {
			for (String to : tos) {
				if (to != null && !to.isEmpty()) {
					this.tos.add(to.trim());
				}
			}
		}

		public void setTos(String[] tos) {
			for (String to : tos) {
				if (to != null && !to.isEmpty()) {
					this.tos.add(to.trim());
				}
			}
		}
		
		public void setTosSplit(String tos){
			for(String to:tos.split("[;,]")){
				if (to != null && !to.isEmpty()) {
					this.tos.add(to.trim());
				}
			}
		}

		public String getSubject() {
			return subject;
		}

		public void setSubject(String subject) {
			this.subject = subject;
		}

		public String getContentText() {
			return contentText;
		}

		public void setContentText(String contentText) {
			this.contentText = contentText;
		}

		public String getAffix() {
			return affix;
		}

		public void setAffix(String affix) {
			this.affix = affix;
		}

		public String getAffixName() {
			return affixName;
		}

		public void setAffixName(String affixName) {
			this.affixName = affixName;
		}

	}
}
